package com.springboot.controller.web;

import com.springboot.dto.WebDoctorViewPatient;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewAppointDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@Controller
public class DoctorOverTimeController {
    @Autowired
    DoctorViewAppointDetailsService doctorViewAppointDetailsService;

    @CrossOrigin
    @RequestMapping(value = "/webgetovertimeorder")
    @ResponseBody
    public String webGetOverTimeOrder(HttpServletRequest request){
        HttpSession session = request.getSession();
        String tel = (String)session.getAttribute("tel");
        System.out.println("WEB:医生查看已逾期预约");
        try {
            DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
            int id=doctorInfoForm.getDoctorId();
            //通过医生ID查找该医生的预约信息
            ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOverOrderByDoctor(id);
            if(orderForms.size()==0)
            {
                return "该医生没有预约信息";
            }
            else
            {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                String current_date=format.format(System.currentTimeMillis());
                int dateNum=0;
                //进度为未就诊的患者id
                int []patientId=new int[orderForms.size()+1];
                //进度为未就诊的预约id
                int []bookId=new int[orderForms.size()+1];
                String []bookDate=new String[orderForms.size()+1];
                String []bookTime=new String[orderForms.size()+1];
                for(int i=0;i<orderForms.size();i++)
                {
                    OrderForm orderForm=orderForms.get(i);
                    System.out.println(orderForm.toTestString());
                        patientId[dateNum]=orderForm.getPatientId();
                        bookId[dateNum]=orderForm.getBootId();
                        bookDate[dateNum]=format.format(orderForm.getBootDate());
                        bookTime[dateNum]=orderForm.getBootTime();
                        dateNum++;
                        System.out.println(orderForm.getBootId());
                }
                //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                ArrayList<WebDoctorViewPatient> doctorViewPatients=new ArrayList<>();
                for(int i=0;i<dateNum;i++)
                {
                    WebDoctorViewPatient doctorViewPatient=new WebDoctorViewPatient();
                    doctorViewPatient.setOrderDate(bookDate[i]);
                    doctorViewPatient.setOrderTime(bookTime[i]);
                    UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                    doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                    doctorViewPatient.setPatientName(userLoginForm.getUserName());
                    doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                    doctorViewPatient.setBookId(bookId[i]);
                    doctorViewPatients.add(doctorViewPatient);
                    System.out.println(doctorViewPatient.toString());
                }

                //存入的是WebDoctorViewPatient
                session.setAttribute("doctorViewPatients3",doctorViewPatients);

                return doctorViewPatients.toString();
            }
        } catch (Exception e) {
            return "页面错误";
        }
    }
}
